home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / AppleScript / Development Tools / Sample Code / 7Edit / Pascal Sources / SVEditFile.p < prev    next >
Encoding:
Text File  |  1994-02-25  |  19.6 KB  |  752 lines  |  [TEXT/MPS ]

  1.  
  2. {[h-,k+,o=100,q+,r+,rec+,t=2,u+,:+,j=15/20/25/30/35/40/45/50/57/1$]} {Pasmat opts!}
  3.  
  4. UNIT SVEditFile;
  5.  
  6. (*
  7.     SVEditFile.p
  8.     
  9.     Version 3.0d8
  10.     
  11.     Copyright © SRL Data 1992, 1993
  12.     
  13.     All rights reserved
  14.     
  15.     Produced by : SRL Data
  16.     Originally Developed for UK.DTS
  17. *)
  18.  
  19. (*
  20.     File Handling routines for the SVEdit example program
  21. *)
  22.  
  23. (*
  24.      This example is brought to you for the purposes of exploration and experimentation in
  25.    System 7.0.  It is not intended to form the basis of your own programs- but try out
  26.    the code - that's what it's there for 
  27. *)
  28.  
  29. {
  30.                                          
  31.     Known problems-:
  32.         
  33.     System 7.0 specifics-:
  34.         Saving a file using a temporary filename generated using a random file name.
  35.         Note the use of PBExchangeFiles to make sure that the FileID of the new file is the
  36.         same as the old one.
  37. }
  38. (*
  39.     Changes for 3.0d2 :
  40.         19-Feb-92    : NH : Fix open of doc with no other docs open and has
  41.                          published sections.
  42.       18-Mar-92    :    NH : AssocAllSections called after SaveAs
  43.         27-Mar-92    : NH : Arrow Cursor on alerts
  44.                      Comment file some more
  45.       28-Mar-92 : NH : GetFileNameToSaveAs - return FALSE if cancelled (was TRUE!)
  46.         
  47.     Changes for 3.0d3:
  48.     
  49.       24-Jun-92 : NH : Deleted unused vars in DoSave,
  50.                          Changed kAEAskUser to kAEAsk
  51.                                          
  52.         26-Jun-92 : NH : Use FSpOpenDF and remove PBHOpen/Zero, make OpenOld return
  53.                          error code.
  54.                                          
  55. *)
  56.   INTERFACE
  57.  
  58.     USES MemTypes, QuickDraw, OSIntf, ToolIntf, Traps, Files, Packages, Editions,AppleEvents,Printing,
  59.  
  60.                  SVEditGlobals, SVEditUtils, SVEditions, SVEditWindow;
  61.                  
  62.     PROCEDURE DoQuit(saveOpt:DescType);
  63.  
  64.     FUNCTION DoClose(aWindow: WindowPtr; canInteract:Boolean; dialogAnswer:DescType): OSErr;
  65.  
  66.     FUNCTION GetFileNameToSaveAs(theDocument:DPtr): OSErr;
  67.  
  68.     FUNCTION DoSave(theDocument : DPtr;
  69.                                         theFSSpec        : FSSpec): OSErr;
  70.  
  71.     FUNCTION GetFileContents(theFSSpec: FSSpec; theDocument:DPtr): OSErr;
  72.  
  73.     PROCEDURE FileError(s, f: STR255);
  74.  
  75.     FUNCTION SaveUsingTemp(theDocument:DPtr): OSErr;
  76.  
  77.     FUNCTION OpenOld(aFSSpec: FSSpec): OSErr;
  78.  
  79.     FUNCTION OpenUsingAlias(theAliasH: AliasHandle): OSErr;
  80.  
  81.     FUNCTION GetFile(VAR theFSSpec: FSSpec): OSErr;
  82.  
  83.     {-----------------------------------------------------------------  }
  84.     { Implementation part                                               }
  85.     {-----------------------------------------------------------------  }
  86.  
  87.   IMPLEMENTATION
  88.         USES AERegistry;
  89.         
  90. {*-----------------------------------------------------------------------
  91.         Name:             FileError
  92.         Purpose:        Puts up an error alert.
  93.     -----------------------------------------------------------------------*}
  94.  
  95.     {$S File}
  96.  
  97.     PROCEDURE FileError(s, f: STR255);
  98.  
  99.       VAR
  100.         alertResult : INTEGER;
  101.                 myErr       : OSErr;
  102.  
  103.       BEGIN
  104.                 myErr := AEInteractWithUser(kAEDefaultTimeOut,
  105.                                                                         NIL,
  106.                                                                         NIL);
  107.         IF (myErr=noErr) THEN
  108.                     BEGIN
  109.                         SetCursor(arrow);
  110.                         ParamText(s, f, '', '');
  111.                 alertResult := Alert(errorAlert, NIL);
  112.                     END;
  113.       END;
  114.  
  115. {*-----------------------------------------------------------------------
  116.         Name:             DoClose
  117.         Purpose:        Closes a window.
  118.     -----------------------------------------------------------------------*}
  119.  
  120.     {$S File}
  121.         FUNCTION DoClose(aWindow : WindowPtr; canInteract : Boolean; dialogAnswer : DescType):OSErr;
  122.             VAR
  123.                 aDocument    : DPtr;
  124.                 alertResult  : INTEGER;
  125.                 theName      : Str255;
  126.                 myErr        : OSErr;
  127.                 
  128.             BEGIN                
  129.                 myErr := noErr;
  130.                 
  131.                 IF (gWCount>0) THEN
  132.                     BEGIN
  133.                         aDocument := DPtrFromWindowPtr(aWindow);
  134.                                 
  135.                         IF (aDocument^.dirty) THEN
  136.                             IF (canInteract AND (dialogAnswer=kAEAsk)) THEN
  137.                                 BEGIN
  138.                                     IF (aDocument^.everSaved = false) THEN
  139.                                         GetWTitle(aWindow, theName) (* Pick it up as a script may have changed it *)
  140.                                     ELSE
  141.                                         theName := aDocument^.theFileName;
  142.                                         
  143.                                     ParamText('Save Changes for ', theName, '', '');
  144.                                     SetCursor(Arrow);
  145.                                     alertResult := Alert(AdviseAlert, nil);
  146.                                     CASE alertResult OF
  147.                                         aaSave  : IF (aDocument^.everSaved = false) THEN
  148.                                                                 BEGIN
  149.                                                                     myErr := GetFileNameToSaveAs(aDocument);
  150.                                                                     IF (myErr = noErr) THEN
  151.                                                                         myErr := DoSave(aDocument, aDocument^.theFSSpec);
  152.                                                                         
  153.                                                                     IF (myErr = noErr) THEN
  154.                                                                         AssocAllSections(aDocument);
  155.                                                                 END
  156.                                                             ELSE
  157.                                                                 myErr := SaveUsingTemp(aDocument);
  158.                                                                         
  159.                                         aaCancel : myErr := userCanceledErr;
  160.                                                                         
  161.                                         aaDiscard: aDocument^.dirty := false;
  162.                                     END
  163.                                 END
  164.                             ELSE
  165.                                 BEGIN
  166.                                     IF (dialogAnswer = kAEYes) THEN
  167.                                         IF (aDocument^.everSaved = false) THEN
  168.                                             BEGIN
  169.                                                 IF (canInteract) THEN
  170.                                                     BEGIN
  171.                                                         myErr := GetFileNameToSaveAs(aDocument);
  172.                                                         IF (myErr = noErr) THEN
  173.                                                             myErr := DoSave(aDocument, aDocument^.theFSSpec);
  174.                                                             
  175.                                                         IF (myErr=noErr) THEN
  176.                                                             AssocAllSections(aDocument);
  177.                                                     END
  178.                                                 ELSE
  179.                                                     myErr := errAENoUserInteraction;
  180.                                             END
  181.                                         ELSE
  182.                                             myErr := SaveUsingTemp(aDocument)
  183.                                     ELSE
  184.                                         myErr := noErr; (* Don't save *)
  185.                                 END;
  186.                         
  187.                         IF (myErr=noErr) THEN
  188.                             BEGIN
  189.                                 IF (aDocument^.numSections<>0) THEN
  190.                                     DeRegisterAllSections(aDocument);
  191.                                 CloseMyWindow(aWindow);
  192.                             END;
  193.                     END
  194.                 ELSE
  195.                     myErr := errAEIllegalIndex;
  196.                     
  197.                 DoClose := myErr;
  198.             END;
  199.  
  200.         {$S File}
  201.         (*
  202.                 DoQuit
  203.                 saveOpt - one of kAEAsk,kAEYes,kAENo
  204.                 if kAEYes or kAEAsk then AEInteactWithUser should have been called
  205.                 before DoQuit. Assumes that it can interact if it needs to.
  206.         *)
  207.         
  208.     PROCEDURE DoQuit(saveOpt : DescType);
  209.  
  210.       VAR
  211.         aWindow       : WindowPtr;
  212.         nextWindow    : WindowPtr;
  213.         nextWPeek     : WindowPeek;
  214.         theKind       : INTEGER;
  215.         check         : OsErr;
  216.  
  217.       BEGIN
  218.         aWindow := FrontWindow;
  219.         WHILE aWindow <> NIL DO
  220.           BEGIN
  221.             nextWPeek := WindowPeek(aWindow)^.nextWindow;
  222.             nextWindow := @nextWPeek^.port;
  223.             IF Ours(aWindow) THEN
  224.               BEGIN
  225.                 check := DoClose(aWindow, TRUE, saveOpt);
  226.                 IF check<>noErr THEN
  227.                   exit(DoQuit);
  228.               END
  229.             ELSE
  230.               BEGIN
  231.                 theKind := WindowPeek(aWindow)^.windowKind;
  232.                 IF theKind < 0 THEN
  233.                   CloseDeskAcc(theKind);
  234.               END;
  235.             aWindow := nextWindow;
  236.           END;     {WHILE/DO loop}
  237.         gQuitting := TRUE;
  238.       END;
  239.  
  240.     FUNCTION GetFile(VAR theFSSpec: FSSpec): OSErr;
  241.       VAR
  242.         myTypes  : SFTypeList;
  243.         reply    : StandardFileReply;
  244.                 myErr       : OSErr;
  245.  
  246.       BEGIN
  247.                 myErr := noErr;
  248.                 
  249.         myTypes[0] := 'TEXT';
  250.  
  251.         StandardGetFile(NIL, 1, myTypes, reply);
  252.  
  253.         IF (reply.sfGood) THEN
  254.                     theFSSpec := reply.sfFile
  255.         ELSE
  256.           myErr := userCanceledErr;
  257.                     
  258.                 GetFile := myErr;
  259.       END;
  260.  
  261.     {$S File}
  262.  
  263.     FUNCTION DoCreate(theSpec: FSSpec): OSErr;
  264.  
  265.       VAR
  266.         err           : OSErr;
  267.  
  268.       BEGIN
  269.  
  270.         err := FSpCreate(theSpec, 'SVED', 'TEXT', smSystemScript);
  271.  
  272.         DoCreate := err;
  273.         IF err <> noErr THEN
  274.           ShowError('DoCreate', err);
  275.       END;
  276.  
  277.     {$S File}
  278.  
  279.     FUNCTION WriteFile(theDocument : DPtr;
  280.                                              refNum             : INTEGER;
  281.                        theFSSpec     : FSSpec): OSErr;
  282.  
  283.       VAR
  284.         resFile       : INTEGER;
  285.         length        : LONGINT;
  286.         theHHandle    : HHandle;
  287.         theSHandle    : StScrpHandle;
  288.         err           : OSErr;
  289.         theAppName    : StringHandle;
  290.                 oldSelStart   : INTEGER;
  291.                 oldSelEnd     : INTEGER;
  292.                 thePHandle    : Handle;
  293.                 
  294.       BEGIN
  295.         WriteFile := 1;
  296.  
  297.         {first write out the text to the data fork}
  298.         length := theDocument^.theText^^.teLength;
  299.         HLock(theDocument^.theText^^.hText);
  300.         IF FSWrite(refNum, length, theDocument^.theText^^.hText^) <> noErr THEN
  301.           exit(WriteFile);
  302.         HUnlock(theDocument^.theText^^.hText);
  303.  
  304.         {we are writing to a temporary file, so we need to create the resource file}
  305.         {before writing out the resources}
  306.         {now open the resource file}
  307.  
  308.         WITH theFSSpec DO
  309.           BEGIN
  310.             HCreateResFile(vRefNum, parID, name);
  311.             err := ResError;
  312.             IF err <> noErr THEN
  313.               BEGIN
  314.                 ShowError('HCreateResFile', err);
  315.                 exit(WriteFile);
  316.               END;
  317.  
  318.             resFile := HOpenResFile(vRefNum, parID, name, fsWRPerm);
  319.             err := ResError;
  320.  
  321.             IF err <> noErr THEN
  322.               BEGIN
  323.                 ShowError('HOpenResFile', err);
  324.                 exit(WriteFile);
  325.               END;
  326.  
  327.           END;
  328.  
  329.         {write out our 'TFSF' resource to file}
  330.                 
  331.                 oldSelStart := theDocument^.theText^^.selStart;
  332.                 oldSelEnd   := theDocument^.theText^^.selEnd;
  333.                 TESetSelect(0,32000, theDocument^.theText);
  334.                 
  335.         theSHandle := GetStylScrap(theDocument^.theText);
  336.                 
  337.                 TESetSelect(oldSelStart,oldSelEnd, theDocument^.theText);
  338.                 
  339.         AddResource(Handle(theSHandle), 'TFSF', 255, 'Style Info');
  340.         err := ResError;
  341.  
  342.         IF err <> noErr THEN
  343.           BEGIN
  344.             ShowError('AddResource- TFSF', err);
  345.             exit(WriteFile);
  346.           END;
  347.                     
  348.                 { write out the printer info }
  349.                 
  350.                 IF (theDocument^.thePrintSetup<> NIL) THEN
  351.                     BEGIN
  352.                         thePHandle := Handle(theDocument^.thePrintSetup);
  353.                         err := HandToHand(thePHandle);
  354.                         
  355.                 AddResource(thePHandle, 'TFSP', 255, 'Printer Info');
  356.                         
  357.                         err := ResError;
  358.         
  359.                         IF err <> noErr THEN
  360.                             BEGIN
  361.                                 ShowError('AddResource- TFSS', err);
  362.                                 exit(WriteFile);
  363.                             END;
  364.                     END;
  365.                 
  366.                 
  367.         theHHandle := HHandle(NewHandle(SizeOf(HeaderRec)));
  368.         HLock(Handle(theHHandle));
  369.  
  370.         GetFontName(theDocument^.theFont, theHHandle^^.theFont);
  371.         theHHandle^^.theSize     := theDocument^.theSize;
  372.         theHHandle^^.theStyle    := theDocument^.theStyle;
  373.         theHHandle^^.lastID      := theDocument^.lastID;
  374.         theHHandle^^.numSections := theDocument^.numSections;
  375.  
  376.         HUnlock(Handle(theHHandle));
  377.  
  378.         AddResource(Handle(theHHandle), 'TFSS', 255, 'Header Info');
  379.  
  380.         err := ResError;
  381.  
  382.         IF err <> noErr THEN
  383.           BEGIN
  384.             ShowError('AddResource- TFSS', err);
  385.             exit(WriteFile);
  386.           END;
  387.  
  388.         {if we have any sections, write out the records and resources}
  389.         IF theDocument^.numSections <> 0 THEN
  390.           BEGIN
  391.             {now write out the section records}
  392.             SaveSections(theDocument);
  393.             
  394.                         {write the latest versions of all editions to their containers}
  395.  
  396.             WriteAllEditions(theDocument);
  397.             {now close the resource file}
  398.             err := ResError;
  399.             IF err <> noErr THEN
  400.               BEGIN
  401.                 ShowError('CloseResFile', err);
  402.                 exit(WriteFile);
  403.               END;
  404.           END;
  405.  
  406.         {Now put an AppName in for Finder in 7.0}
  407.  
  408.         theAppName := StringHandle(NewHandle(6));
  409.         theAppName^^ := '7Edit';
  410.         AddResource(Handle(theAppName), 'STR ', - 16396, 'Finder App Info');
  411.  
  412.         err := ResError;
  413.  
  414.         IF err <> noErr THEN
  415.           BEGIN
  416.             ShowError('AppName', err);
  417.             exit(WriteFile);
  418.           END;
  419.  
  420.         CloseResFile(resFile);
  421.  
  422.         WriteFile := noErr;
  423.       END;
  424.  
  425.     {$S File}
  426.  
  427.     FUNCTION ReadFile(theDocument    : DPtr;
  428.                                             refNum            : INTEGER;
  429.                       fn                    : STR255): OSErr;
  430.  
  431.       VAR
  432.         theSize       : LONGINT;
  433.         resFile       : INTEGER;
  434.         err           : INTEGER;
  435.         aHandle       : HHandle;
  436.                 gHandle                : Handle;
  437.  
  438.       BEGIN
  439.         ReadFile := 1;
  440.  
  441.         IF GetEOF(refNum, theSize) <> noErr THEN
  442.           exit(ReadFile);
  443.  
  444.         {we're only using TE, so check that there is not more than 32K worth of text}
  445.                 
  446.         IF theSize > 32000 THEN
  447.           exit(ReadFile);
  448.  
  449.         gHandle := NewHandle(theSize);
  450.         HLock(gHandle);
  451.                 err := FSRead(refNum, theSize, gHandle^);
  452.                 
  453.         IF err <> noErr THEN
  454.           BEGIN
  455.             HUnlock(gHandle);
  456.                         ReadFile := err;
  457.             exit(ReadFile);
  458.           END;
  459.                                 
  460.         WITH theDocument^.theFSSpec DO
  461.           BEGIN
  462.             resFile := HOpenResFile(vRefNum, parID, fn, fsWRPerm);
  463.                         IF resFile = -1 THEN
  464.                 err := fnfErr;
  465.           END;
  466.                     
  467.                 theDocument^.numSections := 0;
  468.                 
  469.                 IF (err=noErr) THEN
  470.                     BEGIN
  471.                         aHandle := NIL;
  472.         
  473.                         IF Count1Resources('TFSS') <> 0 THEN
  474.                             aHandle := HHandle(Get1Resource('TFSS', 255));
  475.         
  476.                         IF (aHandle <> NIL) THEN
  477.                             theDocument^.numSections := aHandle^^.numSections;
  478.                             
  479.                         (*
  480.                             New Format Info
  481.                         *)
  482.                         
  483.                         aHandle := NIL;
  484.                         IF Count1Resources('TFSF') <> 0 THEN
  485.                             aHandle := HHandle(Get1Resource('TFSF', 255));
  486.                             
  487.                         HLock(gHandle);
  488.                         TEStylInsert(    gHandle^,
  489.                                                     GetHandleSize(gHandle),
  490.                                                     StScrpHandle(aHandle),
  491.                                                     theDocument^.theText);
  492.                                         
  493.                         HUnlock(gHandle);
  494.                         
  495.                         (*
  496.                             If there is a print record saved, ditch the old one
  497.                             created by new document and fill this one in
  498.                         *)
  499.                         IF Count1Resources('TFSP') <> 0 THEN
  500.                             BEGIN
  501.                                 IF (theDocument^.thePrintSetup <> NIL) THEN
  502.                                     DisposHandle(Handle(theDocument^.thePrintSetup));
  503.                                 
  504.                                 theDocument^.thePrintSetup := THPrint(Get1Resource('TFSP', 255));
  505.                               err := HandToHand(Handle(theDocument^.thePrintSetup));
  506.                                 
  507.                                 IF PRValidate(theDocument^.thePrintSetup) THEN;
  508.                             END;
  509.                                                     
  510.                         IF theDocument^.numSections <> 0 THEN
  511.                             BEGIN
  512.                                 ReadSectionRecords(theDocument);
  513.                                 ReadAllSectionResources(theDocument);
  514.                             END;
  515.         
  516.                         CloseResFile(resFile);
  517.         
  518.                         err := ResError;
  519.                         IF err <> noErr THEN
  520.                             BEGIN
  521.                                 ShowError('read file- CloseResFile', err);
  522.                                 exit(ReadFile);
  523.                             END;
  524.           END
  525.                 ELSE
  526.                     TESetText(gHandle^, GetHandleSize(gHandle), theDocument^.theText);
  527.                     
  528.                 IF (gHandle<>NIL) THEN
  529.                     DisposHandle(gHandle);
  530.                     
  531.                 IF err=fnfErr THEN
  532.                     err := noErr;
  533.  
  534.         ReadFile := noErr;
  535.       END; (* ReadFile *)
  536.  
  537. {* -----------------------------------------------------------------------
  538.         Name:             GetFileContents
  539.         Purpose:        Opens the document specified by theFSSpec and puts
  540.                                 the contents into theDocument.
  541.      -----------------------------------------------------------------------*}
  542.  
  543.     {$S File}
  544.  
  545.     FUNCTION GetFileContents(theFSSpec: FSSpec; theDocument:DPtr): OSErr;
  546.  
  547.       VAR
  548.         err           : OSErr;
  549.         theRefNum     : INTEGER;
  550.  
  551.       BEGIN
  552.         GetFileContents := 1; {if it gets through it is set to noErr}
  553.         {this can be called from two places- on an OpenDoc AppleEvent}
  554.         {and by the user just selecting Open from the File Menu}
  555.         {assume that the CFS is correct when the routine is called}
  556.  
  557.         err := FSpOpenDF(theFSSpec,
  558.                                  fsRdWrPerm,
  559.                                                  theRefNum);
  560.         IF err <> noErr THEN
  561.           BEGIN
  562.             GetFileContents := err;
  563.             ShowError('FSpOpenDF', err);
  564.             exit(GetFileContents);
  565.           END
  566.         ELSE
  567.           BEGIN
  568.             IF ReadFile(theDocument, theRefNum, theFSSpec.name) <> noErr THEN
  569.               BEGIN
  570.                 ShowError('ReadFile', err);
  571.                 exit(GetFileContents);
  572.               END;
  573.             IF FSClose(theRefNum) <> noErr THEN
  574.               BEGIN
  575.                 ShowError('FSClose', err);
  576.                 exit(GetFileContents);
  577.               END;
  578.             GetFileContents := noErr;
  579.           END;
  580.       END;
  581.  
  582.     {$S File}
  583.  
  584.     FUNCTION SaveUsingTemp(theDocument : DPtr) : OSErr;
  585.         VAR
  586.             tempName   : Str255;
  587.             err        : OSErr;
  588.             tempFSSpec : FSSpec;
  589.     
  590.         BEGIN
  591.             (*save the file to disk using a temporary file*)
  592.             (*this is the recommended way of doing things*)
  593.             (*first write out the file to disk using a temporary filename*)
  594.             (*if it is sucessfully written, exchange the temporary file with the last one saved*)
  595.             (*then delete the temporary file- so if anything goes wrong, the original version is still there*)
  596.             (*first generate the temporary filename*)
  597.     
  598.             GetTempFileName(theDocument, tempName);
  599.             (*create this file on disk*)
  600.     
  601.             tempFSSpec      := theDocument^.theFSSpec;
  602.             tempFSSpec.name := tempName;
  603.                 
  604.             err := DoCreate(tempFSSpec);    
  605.     
  606.             (*now save the file as normal*)
  607.             
  608.             IF (err=noErr) THEN
  609.                 err := DoSave(theDocument, tempFSSpec);
  610.             
  611.             IF (err = noErr) THEN
  612.                 err := FSpExchangeFiles(tempFSSpec, theDocument^.theFSSpec);
  613.     
  614.             (*we've exchanged the files, now delete the temporary one*)
  615.             
  616.             IF (err = noErr) THEN
  617.                 err := FSpDelete(tempFSSpec);
  618.     
  619.             SaveUsingTemp := err;
  620.         END;
  621.  
  622.     {$S File}
  623.  
  624.     FUNCTION GetFileNameToSaveAs(theDocument:DPtr): OSErr;
  625.             (*
  626.                 Fills in the document record with the user chosen destination
  627.             *)
  628.             
  629.       VAR
  630.         reply         : StandardFileReply;
  631.         err           : OSErr;
  632.                 suggestName   : Str255;
  633.  
  634.       BEGIN
  635.                 GetWTitle(theDocument^.theWindow, suggestName);
  636.             
  637.         StandardPutFile('Save Document As:', suggestName, reply);
  638.  
  639.         IF reply.sfGood THEN
  640.           BEGIN
  641.                 err := FSpDelete(reply.sfFile);
  642.                         
  643.                         IF (err=fnfErr) THEN
  644.                             err := noErr;
  645.                         
  646.                         IF (err = noErr) THEN
  647.                             BEGIN
  648.                                 theDocument^.theFSSpec   := reply.sfFile;
  649.                                 theDocument^.theFileName := reply.sfFile.name;
  650.                             END;
  651.           END
  652.                 ELSE
  653.                     err := userCanceledErr;
  654.                     
  655.                 GetFileNameToSaveAs := err;
  656.       END; (* GetFileNameToSaveAs *)
  657.  
  658.     {$S File}
  659.  
  660.     FUNCTION DoSave(theDocument : DPtr;
  661.                     theFSSpec        : FSSpec): OSErr;
  662.  
  663.       VAR
  664.         refNum        : INTEGER;
  665.                 fileErr                : OSErr;
  666.  
  667.       BEGIN
  668.         DoSave := noErr;
  669.                 
  670.                 fileErr := FSpOpenDF(theFSSpec, fsRdWrPerm, refNum);
  671.                 
  672.                 IF (fileErr = fnfErr) THEN
  673.                     BEGIN
  674.                         fileErr := DoCreate(theFSSpec);
  675.                         
  676.             IF (fileErr<> noErr) THEN
  677.               BEGIN
  678.                                 DoSave := fileErr;
  679.                                 Exit(DoSave);
  680.                             END;
  681.                             
  682.                         fileErr:= FSpOpenDF(theFSSpec, fsRdWrPerm, refNum);
  683.                     END;
  684.  
  685.         IF (fileErr = noErr) THEN
  686.           BEGIN
  687.                         fileErr := WriteFile(theDocument, refNum, theFSSpec);
  688.                         
  689.             IF (fileErr = noErr) THEN
  690.                             theDocument^.dirty := FALSE;
  691.                             
  692.                         fileErr := FSClose(refNum);
  693.           END
  694.         ELSE
  695.           FileError('error opening file ', theFSSpec.name);
  696.                     
  697.           DoSave := fileErr;
  698.             END;
  699.  
  700.     {$S File}
  701.  
  702.     FUNCTION OpenOld(aFSSpec: FSSpec):OSErr;
  703.             VAR theDocument : DPtr;
  704.                     myErr       : OSErr;
  705.                     
  706.       BEGIN
  707.         theDocument := NewDocument(TRUE);
  708.         
  709.                 SetWTitle(theDocument^.theWindow, aFSSpec.name);
  710.                 
  711.                 SetPort(theDocument^.theWindow);
  712.                 
  713.         theDocument^.theFSSpec   := aFSSpec;
  714.         theDocument^.theFileName := aFSSpec.name;
  715.         theDocument^.dirty       := FALSE;
  716.                 theDocument^.everSaved   := TRUE;
  717.  
  718.         myErr := GetFileContents(aFSSpec, theDocument);
  719.                 IF (myErr = noErr) THEN
  720.           BEGIN
  721.                          ResizeWindow(theDocument);
  722.                         ShowWindow(theDocument^.theWindow);
  723.                     END
  724.         ELSE
  725.           BEGIN
  726.                         FileError('Error Opening ', aFSSpec.name);
  727.                     END;
  728.                 
  729.                 OpenOld := myErr;
  730.                 
  731.       END;
  732.  
  733.     {$S File}
  734.  
  735.     FUNCTION OpenUsingAlias(theAliasH: AliasHandle): OSErr;
  736.  
  737.       VAR
  738.         err           : OSErr;
  739.         aFSSpec       : FSSpec;
  740.         dummy         : BOOLEAN;
  741.  
  742.       BEGIN
  743.         OpenUsingAlias := noErr;
  744.         err := ResolveAlias(NIL, theAliasH, aFSSpec, dummy);
  745.         IF (err = noErr) THEN
  746.           err := OpenOld(aFSSpec);
  747.                     
  748.         OpenUsingAlias := err;
  749.       END;
  750.  
  751. END.
  752.